﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;


    public static class EnumHelper
    {
        /// <summary>
        /// Will get the string value for a given enums value, this will
        /// only work if you assign the StringValue attribute to
        /// the items in your enum.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetStringValue(this Enum value)
        {
            // Get the type
            Type type = value.GetType();

            // Get fieldinfo for this type
            FieldInfo fieldInfo = type.GetField(value.ToString());

            // Get the stringvalue attributes
            StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
                typeof(StringValueAttribute), false) as StringValueAttribute[];

            // Return the first if there was a match.
            return attribs.Length > 0 ? attribs[0].StringValue : null;
        }

        public static Enum GetByStringValue(this Enum value, string stringAttr)
        {
            // Get the type
            Type type = value.GetType();

            foreach (string enumName in type.GetEnumNames())
            {              

                // Get fieldinfo for this type
                FieldInfo fieldInfo = type.GetField(enumName);
                if (fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false).Contains(new StringValueAttribute(stringAttr)))
                {
                   return (Enum)Enum.Parse(type,enumName);
                }

            }

            return null;
        }

        public static Enum GetByValue(this Enum enu, string value)
        {
            // Get the type
            Type type = enu.GetType();

            foreach (string enumName in type.GetEnumNames())
            {
                if (enumName == value)
                {
                    return (Enum)Enum.Parse(type, enumName);
                }
            }

            return null;
        }

        public static System.Web.UI.WebControls.ListItemCollection GetEnumList(this Enum enu)
        {
            // Get the type
            Type type = enu.GetType();


            System.Web.UI.WebControls.ListItemCollection returnVal = new System.Web.UI.WebControls.ListItemCollection();
            //List<System.Web.UI.WebControls.ListItem> returnVal = new List<System.Web.UI.WebControls.ListItem>();

            foreach (string enumName in type.GetEnumNames())
            {                

                // Get fieldinfo for this type
                FieldInfo fieldInfo = type.GetField(enumName);
                StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];


                System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem(attribs.Length > 0 ? attribs[0].StringValue : "", enumName);

                returnVal.Add(li);
            }

            return returnVal;

        }
    

    }

    

    /// <summary>
    /// This attribute is used to represent a string value
    /// for a value in an enum.
    /// </summary>
    [AttributeUsage(AttributeTargets.Field)]
    public class StringValueAttribute : Attribute
    {

        #region Properties

        /// <summary>
        /// Holds the stringvalue for a value in an enum.
        /// </summary>
        public string StringValue { get; protected set; }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor used to init a StringValue Attribute
        /// </summary>
        /// <param name="value"></param>
        public StringValueAttribute(string value)
        {
            this.StringValue = value;
        }

        #endregion

    }

